package com.atguigu.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gulimall.ware.vo.MergeVo;
import com.atguigu.gulimall.ware.vo.PurchaseFinishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.ware.entity.WmsPurchaseEntity;
import com.atguigu.gulimall.ware.service.WmsPurchaseService;
import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.R;



/**
 * 采购信息
 *
 * @author yangshen
 * @email yangshen@gmail.com
 * @date 2020-05-26 15:33:03
 */
@RestController
@RequestMapping("/ware/purchase")
public class WmsPurchaseController {
    @Autowired
    private WmsPurchaseService wmsPurchaseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wmsPurchaseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 未领取的采购单
     * 采购单维护 - 采购单
     */
    @RequestMapping("/unreceive/list")
    public R unreceive(@RequestParam Map<String, Object> params){
        PageUtils page = wmsPurchaseService.queryPageUnreceive(params);

        return R.ok().put("page", page);
    }

    /**
     * 合并采购单明细
     * items: [1, 3]
     * purchaseId: 1
     * @param mergeVo
     * @return
     */
    @RequestMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo){
        wmsPurchaseService.mergePurchase(mergeVo);
        return R.ok();
    }

    /**
     * 采购人员领取采购单
     * @param ids 采购单Id
     * @return
     */
    @PostMapping(value = "/received")
    public R received(@RequestBody List<Long> ids){
        wmsPurchaseService.received(ids);
        return R.ok();
    }

    /**
     * 采购人员完成确认采购单
     * @param purchaseFinishVo 采购单入参实体
     * @return
     */
    @PostMapping(value = "/done")
    public R finish(@RequestBody PurchaseFinishVo purchaseFinishVo){
        wmsPurchaseService.finish(purchaseFinishVo);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WmsPurchaseEntity wmsPurchase = wmsPurchaseService.getById(id);

        return R.ok().put("wmsPurchase", wmsPurchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WmsPurchaseEntity wmsPurchase){
        wmsPurchase.setUpdateTime(new Date());
        wmsPurchase.setCreateTime(new Date());
		wmsPurchaseService.save(wmsPurchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WmsPurchaseEntity wmsPurchase){
		wmsPurchaseService.updateById(wmsPurchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wmsPurchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
